package com.dingding.commonservice.core.vehicle.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.dingding.common.core.beans.carinfo.CarBrandInfo;
import com.dingding.common.core.beans.carinfo.CarModelInfo;
import com.dingding.commonservice.core.vehicle.dao.IVehicleDao;
import com.dingding.commonservice.core.vehicle.service.IVehicleService;
import com.dingding.facade.commonservice.common.dto.vehicle.VehicleBrandDTO;
import com.dingding.facade.commonservice.common.dto.vehicle.VehicleModelDTO;

/**
 * 
 * @ClassName: VehicleServiceImpl 
 * @Description: 车辆业务逻辑实现类
 * @author YHQ
 * @date 2016年12月3日 下午4:04:09
 * @since V2.5.7
 * 
 */
@Component("vehicleService")
public class VehicleServiceImpl implements IVehicleService{

	@Resource(name = "vehicleDao")
	private IVehicleDao vehicleDao;
	
	/**
	 * 
	 * @Title: geVehicleBrandList 
	 * @Description: 获取车辆品牌及型号列表 
	 * @author YHQ 
	 * @since V2.5.7
	 * @return List<VehicleBrandDTO>    返回类型
	 * 
	 */
	@Override
	public List<VehicleBrandDTO> geVehicleBrandList() {
		
		// 品牌列表
		List<CarBrandInfo> carBrandInfoList = vehicleDao.getCarBrandInfoList();
		// 型号列表
		List<CarModelInfo> carModelInfoList = vehicleDao.getCarModelInfoList();
		
		return this.geVehicleBrandDTOList(carBrandInfoList, carModelInfoList);
	}

	/**
	 * 
	 * @Title: geVehicleBrandDTOList 
	 * @Description: 获取车辆品牌及类型列表 
	 * @param carBrandInfoList 品牌列表
	 * @param carModelInfoList 类型列表
	 * @author YHQ 
	 * @since V2.5.7
	 * @return List<VehicleBrandDTO>    车辆品牌及类型数据Bean
	 * 
	 */
	public List<VehicleBrandDTO> geVehicleBrandDTOList(List<CarBrandInfo> carBrandInfoList,List<CarModelInfo> carModelInfoList){
		
		// 创建用于存储品牌及类型列表对象
		List<VehicleBrandDTO> vehicleBrandDTOList = null;
		
		// 品牌列表不能为空
		if(carBrandInfoList != null && carBrandInfoList.size() > 0){
			
			// 创建用于存储品牌及类型列表对象
			vehicleBrandDTOList = new ArrayList<VehicleBrandDTO>();
			
			// 遍历品牌列表
			for (CarBrandInfo carBrandInfo : carBrandInfoList) {
				
				// 创建用于存储品牌及类型对象
				VehicleBrandDTO vehicleBrandDTO = new VehicleBrandDTO();
				
				// 品牌Id
				vehicleBrandDTO.setBrandId(carBrandInfo.getId());	
				// 品牌名称
				vehicleBrandDTO.setBrandName(carBrandInfo.getName());
				
				// 车辆类型如果为空，不予遍历赋值
				if(carModelInfoList != null && carModelInfoList.size() > 0){
					
					// 创建用于存储车辆类型列表对象
					List<VehicleModelDTO> vehicleModelDTOList = new ArrayList<VehicleModelDTO>();
					
					// 遍历车辆类型
					for (CarModelInfo carModelInfo : carModelInfoList) {
						
						// 如果该车辆类型属于该品牌，则保存
						if(carBrandInfo.getId().equals(carModelInfo.getBrandId())){
							
							// 创建用于存储车辆型号
							VehicleModelDTO vehicleModelDTO = new VehicleModelDTO();
							
							// 车辆型号Id
							vehicleModelDTO.setModelId(carModelInfo.getId());
							// 车辆型号名称
							vehicleModelDTO.setModelName(carModelInfo.getName());
							
							vehicleModelDTOList.add(vehicleModelDTO);
						}
					}
					
					vehicleBrandDTO.setVehicleModelList(vehicleModelDTOList);
				}
				
				vehicleBrandDTOList.add(vehicleBrandDTO);
			}
			
		}

		return vehicleBrandDTOList;
	}
	
}
